package com.attendance.controller;

import com.attendance.java.DateUtils;
import com.attendance.pojo.Task;
import com.attendance.result.LayuiMiniPage;
import com.attendance.security.user.service.UserService;
import com.attendance.service.DepartmentServiceImpl;
import com.attendance.service.TaskServiceImpl;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/task")
public class TaskController {

    @Autowired
    TaskServiceImpl taskService;
    @Autowired
    DepartmentServiceImpl departmentService;
    @Autowired
    UserService userService;

    @GetMapping()
    public String table() {
        return "/system/task/table";
    }

    @GetMapping("/add")
    @RolesAllowed("ADMIN")
    public String add() {
        return "/system/task/add";
    }

    @GetMapping("/edit/{id}")
    @RolesAllowed("ADMIN")
    public ModelAndView editForId(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/system/task/edit");
        Task task = taskService.selectById(id);
        List<Long> departIds = taskService.getDepartIdsById(task.getId());
        modelAndView.addObject("task", task);
        modelAndView.addObject("startTime", DateUtils.time(task.getStartTime()));
        modelAndView.addObject("endTime", DateUtils.time(task.getEndTime()));
        modelAndView.addObject("departIds", Joiner.on(",").join(departIds));
        modelAndView.addObject("departNames", Joiner.on(",").join(departmentService.getDepartNameByIds(departIds)));
        return modelAndView;
    }

    @GetMapping("/edit")
    @RolesAllowed("ADMIN")
    public String edit() {
        return "/system/user/edit";
    }

    @GetMapping("/binding/{ids}")
    @RolesAllowed("ADMIN")
    public ModelAndView binding(@PathVariable String ids) {
        ModelAndView modelAndView = new ModelAndView("/system/task/binding");
        modelAndView.addObject("ids", ids);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("/listPage")
    public LayuiMiniPage<Task> getListPage(Integer page, Integer limit, String searchParams) {
        if (searchParams != null) {
            Page<Task> listPage = taskService.getListPage(page, limit, searchParams);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        }
        Page<Task> listPage = taskService.getListPage(page, limit);
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }

    @ResponseBody
    @GetMapping("/selectPage")
    public LayuiMiniPage<Task> getListPage(Integer page, Integer limit) {
        Page<Task> listPage = taskService.getSelectPage(page, limit,taskService.getTaskIdsById(userService.getUser().getUserId()));
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }

    @ResponseBody
    @PostMapping("/update")
    @RolesAllowed("ADMIN")
    public Result update(Task task, String departIds) {
        if (taskService.updateById(task,departIds)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PutMapping("/add")
    @RolesAllowed("ADMIN")
    public Result add(Task task,String departIds) {
        try {
            if (taskService.add(task,departIds)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @DeleteMapping("delete")
    @ResponseBody
    @RolesAllowed("ADMIN")
    public Result delete(@RequestParam("ids[]") ArrayList<Integer> ids) {
        if (taskService.delete(ids)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

}
